package by.dkusch.aircompany.webapp.page.tech.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.filter.FilterPlane;
import by.dkusch.aircompany.model.tables.Plane;
import by.dkusch.aircompany.model.tables.Plane_;
import by.dkusch.aircompany.services.PlaneService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.common.SelectorActionPanel;
import by.dkusch.aircompany.webapp.page.base.enums.SelectorType;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.tech.PlanePage;

public class PlaneListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private PlaneService planeService;
	private EntityDataProviderFilter<Long, Plane, FilterPlane> planeDataProvider;

	public PlaneListPanel(String id, int itemsPerPage, final boolean choiseFl) {
		super(id);

		planeDataProvider = new EntityDataProviderFilter<Long, Plane, FilterPlane>(planeService);
		planeDataProvider.setSort(Plane_.code, SortOrder.ASCENDING);
		planeDataProvider.setFilterState(new FilterPlane());

		setOutputMarkupId(true);

		DataView<Plane> dataView = new DataView<Plane>("planeList", planeDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Plane> item) {
				final Plane plane = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("name"));
				item.add(new Label("planeType.code"));
				if (choiseFl) {
					item.add(new SelectorActionPanel<Long>("action-panel", plane.getId(), SelectorType.PLANE));
				} else {
					item.add(new CommonActionPanel<Long, Plane, PlanePage>("action-panel", planeService, plane, PlanePage.class, PlaneListPanel.this));
				}

			}
		};

		FilterForm<FilterPlane> filterForm = new FilterForm<FilterPlane>("filterForm", planeDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(planeDataProvider, "filterState.code")).setLabel(new ResourceModel("plane.list.code")));
		filterForm.add(new TextField<String>("filter-name", PropertyModel.<String> of(planeDataProvider, "filterState.name")).setLabel(new ResourceModel("plane.list.name")));
		filterForm.add(new TextField<String>("filter-type", PropertyModel.<String> of(planeDataProvider, "filterState.typeCode")).setLabel(new ResourceModel("plane.list.type")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("plane-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);

		AddNewItemPanel<Long, Plane, PlanePage> addNewItemPanel = new AddNewItemPanel<Long, Plane, PlanePage>("new-panel", new Plane(), PlanePage.class);
		if (choiseFl) {
			addNewItemPanel.setVisible(false);
		}
		divTable.add(addNewItemPanel);

		divTable.add(new OrderByBorder<SingularAttribute<Plane, ?>>("sortByName", Plane_.name, planeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Plane, ?>>("sortByCode", Plane_.code, planeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Plane, ?>>("sortByType", Plane_.planeType, planeDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);

	}

}
